

import HeadPrefab from "../../component/HeadPrefab";
import DateUtil from "../../engine/DateUtil";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import ViewBase from "../../engine/ViewBase";
import GameController from "../../GameController";
import { HttpHelper } from "../../net/HttpHelper";
import { NativeSdkHelper } from "../../sdk/NativeSdkHelper";
import { WechatSdkManager } from "../../sdk/WechatSdkManager";
import { GameConfig } from "../config/GameConfig";
import { RoomConfig } from "../config/RoomConfig";
import { RoomConfig_cangxian_mj } from "../config/RoomConfig_cangxian_mj";
import { RoomConfig_dazhonger_mj } from "../config/RoomConfig_dazhonger_mj";
import { RoomConfig_ddz } from "../config/RoomConfig_ddz";
import { RoomConfig_huanghua_mj } from "../config/RoomConfig_huanghua_mj";
import { RoomConfig_jianziding } from "../config/RoomConfig_jianziding";
import { RoomConfig_paodekuai } from "../config/RoomConfig_paodekuai";
import { GameResult } from "../model/GameResult";
import { RoomPlayer } from "../model/RoomPlayer";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewGameResult extends ViewBase {

    private gameResult: GameResult;
    private defaultItemNode: cc.Node;

    public onInitChild(gameResult: GameResult, isRecord: boolean = false) {
        this.gameResult = gameResult;
        Logger.log("ViewGameResult=", gameResult)
        this.defaultItemNode = this.findChild(this.node, "playerContainer/defaultItemNode")

        let txtRoomId: cc.Label = this.getChild(this.node, "txtRoomId", cc.Label);
        txtRoomId.string = "房号:" + this.gameResult.room_id
        let txtEndTime: cc.Label = this.getChild(this.node, "txtEndTime", cc.Label)
        txtEndTime.string = DateUtil.formatDateStr(gameResult.time)
        // let confObj: object = gameResult.gameConf

        this.hideAllChildren(this.defaultItemNode.parent)
        let btnFanhui: cc.Node = this.findChild(this.node, "controlUI/btnFanhui")
        UIHelper.onAddEvent(btnFanhui, () => {
            // this.closeView()
            Logger.log("点击返回", gameResult)
            if (gameResult.clubid > 0) {
                GameController.instance.gotoClub(true)
            } else {
                GameController.instance.gotoHall(false)
            }
        })
        let btnShare: cc.Node = this.findChild(this.node, "controlUI/btnShare")
        UIHelper.onAddEvent(btnShare, () => {
            // WechatSdkManager.captureScreenAndShare(0)
            WechatSdkManager.captureScreenAndSaveToAlbum()
        })
        if (isRecord) {
            btnFanhui.active = true
        }else{
            btnFanhui.active = true
        }
        this.refreshPlayerList()
    }

    private refreshPlayerList() {
        Logger.log("refreshPlayerList==11=",this.gameResult)
        for (let i = 0; i < this.gameResult.playerList.length; i++) {
            let player: RoomPlayer = this.gameResult.playerList[i]
            let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
            if (!itemNode) {
                itemNode = cc.instantiate(this.defaultItemNode)
                itemNode.parent = this.defaultItemNode.parent
            }
            itemNode.active = true
            let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
            Logger.log("refreshPlayerList==22=", player, player.showImgIndex, player.score)
            head.setHead(player.headimg, 0)
            // if (!player.isGetInfo) {
            //     HttpHelper.get_user_data(player.userid, (data: Object) => {
            //         player.isGetInfo = true
            //         player.img1 = data["img1"]
            //         player.headimg = data["headimg"]
            //         if (player.img1 && player.img1.includes("http") && player.showImgIndex == 0 && player.score > 0) {
            //         } else {
            //             head.setHead(player.headimg, 0)
            //         }
            //     })
            // } else {
            //     if (player.img1 && player.img1.includes("http") && player.showImgIndex == 0 && player.score > 0) {
            //         head.setHead(player.img1, 0)
            //     } else {
            //         head.setHead(player.headimg, 0)
            //     }
            // }
            UIHelper.onAddEvent(head.node, () => {
                this.onChangePlayerImg(player)
            })
            // let btnExchange: cc.Node = this.findChild(itemNode, "btnExchange")
            // UIHelper.onAddEvent(btnExchange, () => {
            //     this.onChangePlayerImg(player)
            // })
            let txtId: cc.Label = this.getChild(itemNode, "txtId", cc.Label)
            txtId.string = "ID:" + player.userid
            let txtNickName: cc.Label = this.getChild(itemNode, "txtNickName", cc.Label)
            txtNickName.string = player.nick_name;
            // let txtinvite: cc.Label = this.getChild(itemNode, "txtinvite", cc.Label)
            // if (player.invite && player.invite > 0) {
            //     HttpHelper.get_user_data(player.invite, (data: Object) => {
            //         txtinvite.string = "" + player.invite + " " + data["nick_name"];
            //     })
            // } else {
            //     txtinvite.string = "推广码:"
            // }
            let txtScore: cc.Label = this.getChild(itemNode, "txtScore", cc.Label)
            if (player.score >= 0) {
                txtScore.string = "+" + player.score
                txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[0]
            } else {
                txtScore.string = "" + player.score
                txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[1]
            }
            let icon_dayingjia: cc.Node = this.findChild(itemNode, "icon_dayingjia")
            if (this.gameResult.bigWin == player.userid) {
                icon_dayingjia.active = true
            } else {
                icon_dayingjia.active = false
            }
            let txtZimo: cc.Label = this.getChild(itemNode, "txtZimo", cc.Label)
            txtZimo.string = "" + player.zimo
            let txtJiepao: cc.Label = this.getChild(itemNode, "txtJiepao", cc.Label)
            txtJiepao.string = "" + player.jiepao
            let txtDianpao: cc.Label = this.getChild(itemNode, "txtDianpao", cc.Label)
            txtDianpao.string = "" + player.dianpao
            let txtModou: cc.Label = this.getChild(itemNode, "txtModou", cc.Label)
            txtModou.string = "" + player.gangTimes
            let txtMoji: cc.Label = this.getChild(itemNode, "txtMoji", cc.Label)
            txtMoji.string = "" + player.jiTimes
        }
    }

    private onChangePlayerImg(player: RoomPlayer) {
        player.showImgIndex++
        if (player.showImgIndex > 1) {
            player.showImgIndex = 0
        }
        this.refreshPlayerList()
    }

    onDisable(): void {
        super.onDisable()
        GameResult.instance = null

    }

}
